package pack.projectiles;

import pack.characters.Entity;
import pack.characters.EntityManager;

import com.jme.bounding.BoundingBox;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.scene.Spatial.CullHint;
import com.jme.scene.shape.Sphere;
import com.jmex.physics.DynamicPhysicsNode;
import com.jmex.physics.PhysicsSpace;

public class Projectile extends Entity 
{
    private float speed = 20;
    private float lifeTime = 5;    
    private float age = 5;
    private Vector3f direction = null;
    private boolean active;

    public Projectile(final PhysicsSpace physicsSpace) 
    {
    	super(physicsSpace, null, null, null, 1, true);
    	node.setName("projectile");
        health = 1;
    }
    
    @Override
    protected void initModel() 
    {
        model = new Sphere("projectil Model", 5, 5, 0.2f);
        model.setModelBound(new BoundingBox());
        model.updateModelBound();
    }
    
    public void fire(final Vector3f direction, final Vector3f startLocation,  final Quaternion rotation) 
    {
    	this.direction = direction.normalize();
    	node.getLocalTranslation().set(startLocation);
    	node.getLocalRotation().set(rotation);
    	    	
        node.setCullHint(CullHint.Dynamic);
    	node.updateGeometricState(0, false);
    	((DynamicPhysicsNode)node).clearDynamics();
    }

    public void die() {
    	EntityManager.get().remove(this);
    	this.node.delete();
    	if (node.getControllerCount() > 0)
    	    this.node.removeController(0);
        this.active = false;
    }

    public final float getLifeTime() {
        return lifeTime;
    }


    public final void setLifeTime(final float lifeTime) {
        this.lifeTime = lifeTime;
    }


    public final float getSpeed() {
        return speed;
    }


    public final void setSpeed(final float speed) {
        this.speed = speed;
    }


    public final Vector3f getDirection() {
        return direction;
    }

    public void setActive(boolean active) {
		this.active = active;
	}


	public boolean isActive() {
		return active;
	}

    public float getAge() {
        return age;
    }

    public void setAge(float age) {
        this.age = age;
    }
}
